package com.obtk.property.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.obtk.property.dao.ParkingMapper;
import com.obtk.property.pojo.Parking;
import com.obtk.property.service.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;
import java.util.Map;

@Service
public class ParkingServiceImpl implements ParkingService {

    @Autowired
    private ParkingMapper parkingMapper;

    @Override
    public List<Parking> sel(Integer sid) {
        return parkingMapper.sel(sid);
    }

    @Override
    public List<Parking> selAll() {
        return parkingMapper.selectAll();
    }

    @Override
    public Page<Parking> search(Map searchMap) {
        Example example = new Example(Parking.class);
        int pageNum =1;
        int pageSize=2;
        if(searchMap !=null) {
            Example.Criteria criteria = example.createCriteria();
            if (StringUtil.isNotEmpty((String) searchMap.get("startTime"))) {
                criteria.andGreaterThanOrEqualTo("createTime", searchMap.get("startTime"));
            }
            if (StringUtil.isNotEmpty((String) searchMap.get("endTime"))) {
                criteria.andGreaterThanOrEqualTo("createTime", searchMap.get("endTime"));
            }
            if (StringUtil.isNotEmpty((String) searchMap.get("ownerName"))) {
                criteria.andLike("ownerName", "%" + (String) searchMap.get("ownerName") + "%");
            }
            //分页

            if((Integer) searchMap.get("pageNum") !=null){
                pageNum = (Integer) searchMap.get("pageNum");
            }
            if((Integer) searchMap.get("pageSize") !=null){
                pageSize = (Integer) searchMap.get("pageSize");
            }
        }
        PageHelper.startPage(pageNum,pageSize);//使用PageHelper插件完成分页
        Page<Parking> parking = (Page<Parking>) parkingMapper.selectByExample(example);
        return parking;
    }

    @Override
    public Boolean add(Parking parking) {
        int row = parkingMapper.insert(parking);
        if (row>0){
            return true;
        }
        return false;
    }

    @Override
    public Parking findById(Integer parkingId) {
        return parkingMapper.selectByPrimaryKey(parkingId);
    }

    @Override
    public Boolean update(Parking parking) {
        int row = parkingMapper.updateByPrimaryKeySelective(parking);
        if(row>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateStatus(String useType, Integer parkingId) {
        Parking parking = new Parking();
        parking.setParkingId(parkingId);
        parking.setUseType(useType);
        int row = parkingMapper.updateByPrimaryKeySelective(parking);
        if(row>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean del(List<Integer> ids) {
        for (Integer id:ids){
            parkingMapper.deleteByPrimaryKey(id);
        }
        return true;
    }

//    @Override
//    public List<Parking> sel(Integer sid) {
//        return mapper.sel(sid);
//    }
}
